<?php
# Pagelist1 :  共22 項   顯示第 1 至 8 項
# Pagelist2 :   <  下一頁 最終頁 > 
# Pagelist3 1 | 2 | 3 下一頁 最終頁

#共 200 個，顯示第 31至 40個 
#共20頁，跳至 []
#上一頁 4 - 5 - 6 - 7 - 8 - 9 - 10 - 11 - 12 - 13 - 下一頁



//<div class="pagination">
//<div class="index">
//<a href="rmclist.html?type=interest&amp;page=3&amp;filter=all&amp;" class="prev">上一頁</a>
//
//4 - <a href="rmclist.html?type=interest&amp;page=5&amp;filter=all&amp;" title="結果">5</a> -
//<a href="rmclist.html?type=interest&amp;page=6&amp;filter=all&amp;" title="結果">6</a> -
//<a href="rmclist.html?type=interest&amp;page=7&amp;filter=all&amp;" title="結果">7</a> -
//<a href="rmclist.html?type=interest&amp;page=8&amp;filter=all&amp;" title="結果">8</a> -
//
//<a href="rmclist.html?type=interest&amp;page=9&amp;filter=all&amp;" title="結果">9</a> -
//<a href="rmclist.html?type=interest&amp;page=10&amp;filter=all&amp;" title="結果">10</a> -
//<a href="rmclist.html?type=interest&amp;page=11&amp;filter=all&amp;" title="結果">11</a> -
//<a href="rmclist.html?type=interest&amp;page=12&amp;filter=all&amp;" title="結果">12</a> -
//<a href="rmclist.html?type=interest&amp;page=13&amp;filter=all&amp;" title="結果">13</a> -
//
//<a href="rmclist.html?type=interest&amp;page=5&amp;filter=all&amp;" class="next">下一頁</a>
//
//</div>
//
//<span class="total">共20頁，跳至</span>
//<form action="rmclist.html" method="post">
//<input name="type" value="interest" type="hidden"><input name="filter" value="all" type="hidden"><input name="err" value="" type="hidden"><input name="query_string" value="?type=interest&amp;page=4&amp;filter=all&amp;" type="hidden"><input name="so_genmatch" value="MF" type="hidden"><input name="so_lo_age" value="25" type="hidden"><input name="so_up_age" value="30" type="hidden"><input name="hasphoto" value="Y" type="hidden"><input name="so_lo_hei" value="66" type="hidden"><input name="so_up_hei" value="72" type="hidden"><input name="so_lo_body" value="2" type="hidden"><input name="so_up_body" value="4" type="hidden"><input name="page" class="text" type="text">
//<input value="執行" title="執行" class="sbtn" type="submit"></form>
//</div>


class Pagelist{
  private $page;
  private $pagesize;
  private $first_pagesize;
  private $total;
  private $first_pageoffset;
  private $unit;
  private $is_static;
  private $base_url;
  private $querystr;
  
  # About Pagelist 2
  private $start_page2 = '<';
  private $end_page2   = '>';
  private $first_page2 = '';
  private $last_page2  = '';
  private $prev_page2 ;
  private $next_page2 ;
  private $pagelist_middle2;
  
  # About Pagelist 3
  private $first_page3= '';
  private $last_page3 = '';
  private $prev_page3 ;
  private $next_page3 ;
  private $windows_size_page3;
  private $curr_page3;
  
  
	//$h_param = array('page'=> 10, 'pagesize'=>'', ...); 
  #function __construct( $h_param=array() ){
  function set_param($h_param=array() ){
    // fixme ??? check hash param
	  $this->page             = get_hash_param($h_param, 'page',              1     );
	  $this->pagesize         = get_hash_param($h_param, 'pagesize',          10    );	  
	  $this->first_pagesize   = get_hash_param($h_param, 'first_pagesize',    $this->pagesize);
	  $this->total            = get_hash_param($h_param, 'total',             0);	  
	  $this->first_pageoffset = get_hash_param($h_param, 'first_pageoffset',  0);
	  $this->unit             = get_hash_param($h_param, 'unit',              locale_string('Pagelist Unit'));
	  $this->is_static        = get_hash_param($h_param, 'is_static',         0);
	  $this->base_url         = get_hash_param($h_param, 'base_url',          ''); # for both static page / dynamic page
	  $this->querystr         = get_hash_param($h_param, 'query_str',          '1=1'); # for dynamic url, 
	  
	  $h_format = get_hash_param($h_param, 'format',            array());
	        
	  // page formatting 
  	
	  $this->start_page2        = get_hash_param($h_format, 'STARTPAGE2',       '<' );
    $this->end_page2          = get_hash_param($h_format, 'ENDPAGE2',         '>' );
    $this->first_page2        = get_hash_param($h_format, 'FIRSTPAGE2',       locale_string('First Page')   );
    $this->last_page2         = get_hash_param($h_format, 'LASTPAGE2',        locale_string('Last Page')    );
    $this->prev_page2         = get_hash_param($h_format, 'PREVPAGE2',        locale_string('Previous Page'));
    $this->next_page2         = get_hash_param($h_format, 'NEXTPAGE2',        locale_string('Next Page')    );
    $this->pagelist_middle2   = get_hash_param($h_format, 'PAGELISTMID2',      ''                           );
    $this->first_page3        = get_hash_param($h_format, 'FIRSTPAGE3',       locale_string('First Page')   );
    $this->last_page3         = get_hash_param($h_format, 'LASTPAGE3',        locale_string('Last Page')    );
    $this->prev_page3         = get_hash_param($h_format, 'PREVPAGE3',        locale_string('Previous Page'));
    $this->next_page3         = get_hash_param($h_format, 'NEXTPAGE3',        locale_string('Next Page')    );
    $this->windows_size_page3 = get_hash_param($h_format, 'WINDOWSIZEPAGE3',  '5'                           );  
    $this->curr_page3         = get_hash_param($h_format, 'CURRPAGE3',        ''                            );
   	$this->unit               = get_hash_param($h_format, 'UNIT',             locale_string('Pagelist Unit'));
    
	  return true;
	} // end __contructor

//  // Function copied from cgiapp
//  function is_assoc_array($php_val = null) {
//    if (!is_array($php_val))  return false;
//    return array_keys($php_val) !== range(0, count($php_val) - 1);
//  }  
	 
	function limit_base_2(){
		return self::limit_base($this->page, $this->pagesize, $this->first_pagesize, $this->first_pageoffset);
	}  
	
	static function limit_base($page, $psize, $first_psize, $offset){	  
	  if ($first_psize == $psize){
	    return array( ($page - 1) * $psize + $offset, $psize);
	  } else {
	  	if ($page == 1){
	  		return array(0 + $offset, $first_psize);
	  	} else {
	  	  return array( ($page - 2) * $psize + $first_psize + $offset, $psize );
	    }
	  }
  }


  
  
  function gen($required_pagelist = '1,2,3'){
  	$page             = $this->page             ;
	  $pagesize         = $this->pagesize         ;
	  $first_pagesize   = $this->first_pagesize   ; # first page can have more / less item in page
	  $total            = $this->total            ;
	  $first_pageoffset = $this->first_pageoffset ;
	  $unit             = $this->unit             ;
	  $is_static        = $this->is_static        ;
	  $base_url         = $this->base_url         ;
	  $querystr         = $this->querystr         ;
  	
    $prev_base = $page - $this->windows_size_page3;
    $next_base = $page + $this->windows_size_page3;
	
	  // common for static & dynamic pagelist
	  // part 1 #
	  
	  list ($limit_base, $limit_pagesize) = $this->limit_base_2();
	  
	  #$show_offset_mode = 1;
	  
	  //error_log("OFFSET : $offset ($limit_base, $limit_pagesize)\n";
	  
	  $rec_start = ($page - 1) * $pagesize + 1 ;
	  $rec_end   = ($page) * $pagesize;
	  
	  if ($pagesize != $first_pagesize){
	  	if ($page == 1){
	  		$rec_start = 1;
	  		$rec_end   = $first_pagesize;
	  	} else {
	  		$rec_start = ($page - 2) * $pagesize + $first_pagesize + 1;
	  		$rec_end   = ($page - 1) * $pagesize + $first_pagesize;
	  	}
	  	
	  }
	  
	  # the main 3 variable to hold the generated pagelist
	  $pagelist1 = '';
	  $pagelist2 = '';
	  $pagelist3 = '';
	  
	  
	  if ($rec_end > $total) $rec_end   = $total ;
	  $total_offset = 0; 
	  #if ($show_offset_mode == 1){
	  #	$tmp = $total - $offset;
	  #  $pagelist1 = locale_string('Total')." $tmp $unit";
	  #} else {
	    $pagelist1 = locale_string('Total')."$total $unit";
	  #}
	  
	  if ($total > 0){
	  	if ($pagesize > 1){
	      $pagelist1 .= '&nbsp;&nbsp;&nbsp;'.locale_string('Pagelist Showing')." $rec_start ".locale_string('Pagelist to')." $rec_end $unit";
	    } else {
	    	$pagelist1 .= '&nbsp;&nbsp;&nbsp;'.locale_string('Pagelist Showing')." $rec_start $unit";
	    }
	  }
	  
	#  my $noofp = int( ($total + $pagesize - 1)/$pagesize);
	#
	#  if ($pagesize != $first_pagesize){
	#    $noofp = int( ($total + $first_psize - 1)/$pagesize);
	#  }
	
	  $noofp = 0;
	  if ($total <= $first_pagesize){
	  	$noofp = 1;
		} else {
		  $tmp_total = $total - $first_pagesize - $total_offset;
		  $noofp = floor(($tmp_total + $pagesize - 1)/$pagesize) + 1;
		}
	  
	
		if ($prev_base < 1) $prev_base = 1;
		if ($next_base > $noofp) $next_base = $noofp ;
	
	
	  if ($is_static != 1) $querystr .= "&total=$total" ;
	
	  $a_list3 = array();
	  # part 3 #
	
	  
	  for($i=$prev_base; $i<=$next_base; $i++) {
	    if ($i == $page ) {
	    	if ($this->curr_page3 != ''){
	        array_push($a_list3, "<span class=\"$this->curr_page3\">$i</span>");
	      } else {
	        array_push($a_list3, "$i");
	      }
	    } else {
	    	if ($is_static != 1){
	        array_push($a_list3, "<a href=\"$base_url?$querystr&page=$i\">$i</a>");
	      } else {
	      	array_push($a_list3, sprintf("<a href=\"$base_url\">%d</a>", $i,$i));
	      }
	    }
	  }
	
	  $tmp;
	  if ($page > 1) {
	  	$tmp = $page-1;
	  	if ($is_static != 1){
		  	$pagelist2 = "<a href=\"$base_url?$querystr&page=1\">$this->first_page2</a>&nbsp;<a href=\"$base_url?$querystr&page=$tmp\">$this->prev_page2</a>&nbsp;";
		  	$pagelist3 = "<a href=\"$base_url?$querystr&page=1\">$this->first_page3</a>&nbsp;<a href=\"$base_url?$querystr&page=$tmp\">$this->prev_page3</a>&nbsp;";
	  	} else {
		  	$pagelist2 = sprintf("<a href=\"$base_url\">$this->first_page2</a>&nbsp;<a href=\"$base_url\">$this->prev_page2</a>&nbsp;", 1,$tmp);
		  	$pagelist3 = sprintf("<a href=\"$base_url\">$this->first_page3</a>&nbsp;<a href=\"$base_url\">$this->prev_page3</a>&nbsp;", 1,$tmp);
		}
	  }
	
	  $pagelist2 .= $this->pagelist_middle2;
	
	  $pagelist3 .= implode(' | ', $a_list3);
	
#	  $pagelist4 .= implode(' - ', $a_list3);	
	
	  if ($page < $noofp) {
	   $tmp = $page+1;
	   if ($is_static != 1){
				$pagelist2 .= "&nbsp;<a href=\"$base_url?$querystr&page=$tmp\" >$this->next_page2</a>&nbsp;<a href=\"$base_url?$querystr&page=$noofp\">$this->last_page2</a>";
				$pagelist3 .= "&nbsp;<a href=\"$base_url?$querystr&page=$tmp\" >$this->next_page3</a>&nbsp;<a href=\"$base_url?$querystr&page=$noofp\">$this->last_page3</a>";
	   } else {
	  		$pagelist2 .= sprintf("&nbsp;<a href=\"$base_url\">$this->next_page2</a>&nbsp;<a href=\"$base_url\">$this->last_page2</a>", $tmp,$noofp);
	   		$pagelist3 .= sprintf("&nbsp;<a href=\"$base_url\">$this->next_page3</a>&nbsp;<a href=\"$base_url\">$this->last_page3</a>", $tmp,$noofp);
			}
	  }
	
	  if ($pagelist2 != '') $pagelist2 = "$this->start_page2 $pagelist2 $this->end_page2" ;
	
		//terry
		$pageall['total'] = $total;
		$pageall['pagecount'] = $noofp;
		$pageall['curpage'] = $page;
	
	  return array($pagelist1, $pagelist2, $pagelist3, $pageall);  	
  }


  # gen a pagelist when no result
  function gen_empty(){ 
   	return array('','','', array('total'=> 0, 'pagecount'=> 0, 'curpage'=>1));
  } 
  
}	



?>
